import axios from 'axios'
import { ElMessage } from 'element-plus'

//创建axios实例
const service = axios.create({
  baseURL: 'http://localhost:1020',
  timeout: 20000
});

//request控制器
service.interceptors.request.use(
  config => {
    return config
  },
  error =>{
    Promise.reject(error);      //Promise是vue3异步编程的核心工具，能够处理网络请求、数据加载等需要等待的任务
  }
)

//response拦截器
service.interceptors.response.use(
  response =>{
    const res = response.data;
    if(res.code < 0){
      ElMessage({
        message: res.message,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject('error...');
    }else{
      return response.data
    }
  },
  error =>{
    ElMessage({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error);
  }
)

